# 开奖信息分析
from loaddata import modelKJ as db
import pandas as pd
from analysis import util

def analysisT(year):
    ds = db.getHistoryDataFromYear(year)
    rs = []
    # 分析
    for a in ds:
        fenxi = {"pei": 1}
        m = {"base_info":a.get("base_info"),"kaij":a.get("kaij"),"fenxi":fenxi}
        rs.append(m)
    return rs

def analysis(year,rang):
    ds = db.getHistoryDataFromYear(year)
    # 找到让球一赔小于1.5的数据(让一球)
    min_rs = get_min_datas(1.5,ds,rang)
    # 找主客差最小的
    zkc = get_zkc_min(min_rs)
    # 开奖标记
    kjbj = to_KJ_Flag(zkc)
    rs = []
    # 分析
    for a in kjbj:
        fenxi = {"pei": 1}
        m = {"base_info":a.get("base_info"),"kaij":a.get("kaij"),"fenxi":fenxi}
        rs.append(m)
    return rs


#   找到让球一赔小于a的数据(让一球)
def get_min_datas(minps,ds,rang):
    rs = []
    # 分析
    for m in ds:
        HHAD = m.get("kaij").get("HHAD")
        if HHAD:
            ps = HHAD.get("ps")[-1]
            goalLine = ps.get("goalLine")
            if goalLine == rang:
                a = float(ps.get("a"))
                d = float(ps.get("d"))
                h = float(ps.get("h"))
                plist = [a,d,h]
                # 小到大的排序，方便找到最小的那个
                splist = sorted(plist,reverse=False)
                # 第一个就是最小的
                min = splist[0]
                # 找到小于某个赔率的数据
                if min <= minps:
                    rs.append(m)
    return rs


# 找到胜平负主客差最小的
def get_zkc_min(ds):
    rs = []
    # 分析
    for m in ds:
        # 先计算主客差
        HAD = m.get("kaij").get("HAD")
        if HAD:
            ps = HAD.get("ps")[-1]
            a = float(ps.get("a"))
            h = float(ps.get("h"))
            m["zkc"]=abs(a-h)
            m["matchTime"] = m.get("base_info").get("matchTime")
            rs.append(m)
    # 根据日期分组
    min_ds = get_min_by_day_group(ds)
    return min_ds

# 根据日期分组并返还差值最小的一个
def get_min_by_day_group(ds):
    # 按照时间分组  每天的再按照低赔从低到高的排序，并将每组转换为词典
    df = pd.DataFrame(ds)
    grouped_dicts = df.groupby('matchTime').apply(lambda x: x.to_dict('records')).to_dict()
    min_ds = []
    for key in grouped_dicts:
        tt = sorted(grouped_dicts.get(key), key=lambda e: e["zkc"], reverse=False)
        min_ds.append(tt[0])

    sorted_list = sorted(min_ds, key=lambda x: x['matchTime'],reverse=True)
    return sorted_list

# 开奖标记
def to_KJ_Flag(ds):
    rs = []
    for m in ds:
        HAD = m.get("kaij").get("HAD")
        m.get("kaij")["HAD"] = toHAD(HAD)
        HHAD = m.get("kaij").get("HHAD")
        m.get("kaij")["HHAD"] = toHAD(HHAD)
        rs.append(m)
    return rs

def toHAD(had):
    if had:
        combination = had.get("combination").lower()  # 开奖标记，要转换小写
        ps = had.get("ps")[-1]
        a = ps.get("a")
        d = ps.get("d")
        h = ps.get("h")
        had["rs"] = [("h", h, combination), ("d", d, combination), ("a", a, combination)]
        spf = {"h": float(h), "d": float(d), "a": float(a)}
        peid_spf = util.to_pei_list(spf)
        had["pei"] = {"combination": combination, "peilist": peid_spf}
    return had